package dynamic.mapper;


import com.baomidou.mybatisplus.core.metadata.IPage;
import dynamic.DynamicEntity;
import dynamic.mybatisplus.core.conditions.query.DQueryWrapper;
import dynamic.mybatisplus.core.conditions.query.DWrapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.exceptions.TooManyResultsException;
import org.apache.ibatis.session.ResultHandler;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

public interface DBaseMapper extends DMapper {

    int insert(DynamicEntity entity);

    int deleteById(Serializable id);

    
    int deleteById(DynamicEntity entity);

    default int deleteByMap(DQueryWrapper qw,Map<String, Object> columnMap) {
         
        return this.delete((DWrapper)qw.allEq(columnMap));
    }

    int delete(@Param("ew") DWrapper queryWrapper);

    int deleteBatchIds(@Param("coll") Collection<?> idList);

    int updateById(@Param("et") DynamicEntity entity);

    int update(@Param("et") DynamicEntity entity, @Param("ew") DWrapper updateWrapper);

    default int update(@Param("ew") DWrapper updateWrapper) {
        return this.update((DynamicEntity) null, updateWrapper);
    }

    DynamicEntity selectById(Serializable id);

    List<DynamicEntity> selectBatchIds(@Param("coll") Collection<? extends Serializable> idList);

    void selectBatchIds(@Param("coll") Collection<? extends Serializable> idList, ResultHandler<DynamicEntity> resultHandler);

    default List<DynamicEntity> selectByMap(DQueryWrapper qw,Map<String, Object> columnMap) {
         
        return this.selectList((DWrapper)qw.allEq(columnMap));
    }

    default void selectByMap(DQueryWrapper qw,Map<String, Object> columnMap, ResultHandler<DynamicEntity> resultHandler) {
        this.selectList(qw.allEq(columnMap), resultHandler);
    }

    default DynamicEntity selectOne(@Param("ew") DWrapper queryWrapper) {
        return this.selectOne(queryWrapper, true);
    }

    default DynamicEntity selectOne(@Param("ew") DWrapper queryWrapper, boolean throwEx) {
        List<DynamicEntity> list = new ArrayList();
        this.selectList(queryWrapper, (resultContext) -> {
            DynamicEntity resultObject = resultContext.getResultObject();
            list.add(resultObject);
            if (!throwEx || resultContext.getResultCount() > 1) {
                resultContext.stop();
            }

        });
        int size = list.size();
        if (size == 1) {
            return list.get(0);
        } else if (size > 1) {
            if (throwEx) {
                throw new TooManyResultsException("Expected one result (or null) to be returned by selectOne(), but found multiple records");
            } else {
                return list.get(0);
            }
        } else {
            return null;
        }
    }

    default boolean exists(DWrapper queryWrapper) {
        Long count = this.selectCount(queryWrapper);
        return null != count && count > 0L;
    }

    Long selectCount(@Param("ew") DWrapper queryWrapper);

    List<DynamicEntity> selectList(@Param("ew") DWrapper queryWrapper);

    void selectList(@Param("ew") DWrapper queryWrapper, ResultHandler<DynamicEntity> resultHandler);

    List<DynamicEntity> selectList(IPage<DynamicEntity> page, @Param("ew") DWrapper queryWrapper);

    void selectList(IPage<DynamicEntity> page, @Param("ew") DWrapper queryWrapper, ResultHandler<DynamicEntity> resultHandler);

    List<Map<String, Object>> selectMaps(@Param("ew") DWrapper queryWrapper);

    void selectMaps(@Param("ew") DWrapper queryWrapper, ResultHandler<Map<String, Object>> resultHandler);

    List<Map<String, Object>> selectMaps(IPage<? extends Map<String, Object>> page, @Param("ew") DWrapper queryWrapper);

    void selectMaps(IPage<? extends Map<String, Object>> page, @Param("ew") DWrapper queryWrapper, ResultHandler<Map<String, Object>> resultHandler);

    <E> List<E> selectObjs(@Param("ew") DWrapper queryWrapper);

    <E> void selectObjs(@Param("ew") DWrapper queryWrapper, ResultHandler<E> resultHandler);

    default <P extends IPage<DynamicEntity>> P selectPage(P page, @Param("ew") DWrapper queryWrapper) {
        List<DynamicEntity> list = this.selectList(page, queryWrapper);
        page.setRecords(list);
        return page;
    }

    default <P extends IPage<Map<String, Object>>> P selectMapsPage(P page, @Param("ew") DWrapper queryWrapper) {
        List<Map<String, Object>> list = this.selectMaps(page, queryWrapper);
        page.setRecords(list);
        return page;
    }
}
